package ml.tewas.commondata;

import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ml.tewas.gui.DrawingCanvas;
import ml.tewas.threatevaluation.IThreatEvaluationNeuralNetwork;
import ml.tewas.threatevaluation.ThreatEvaluationNeuralNetwork;
import ml.tewas.util.Utilities;
import ml.tewas.weaponassignment.IWeaponAssignmentDecisionMaker;
import ml.tewas.weaponassignment.WeaponAssignmentDecisionMaker;

public class ForceStruct implements IForceStruct {

	private int agentID;

	private BoardCell position;

	private ForceEnum forceType;

	private int weaponCount;

	private BufferedImage image;
	
	private boolean destroyed = false;
	
	DrawingCanvas instance = DrawingCanvas.getInstance();
	
	IThreatEvaluationNeuralNetwork threatEvaluationNeuralNetwork = null;
	
	IWeaponAssignmentDecisionMaker weaponAssignmentDecisionMaker = null;
	
	public ForceStruct(int agentID, BoardCell position, ForceEnum forceType,
			int weaponCount, BufferedImage image, boolean isTraining) {
		super();
		this.agentID = agentID;
		this.position = position;
		this.forceType = forceType;
		this.weaponCount = weaponCount;
		this.image = image;
		threatEvaluationNeuralNetwork = new ThreatEvaluationNeuralNetwork(this);
		threatEvaluationNeuralNetwork.setTraining(isTraining);
		weaponAssignmentDecisionMaker = new WeaponAssignmentDecisionMaker();
		weaponAssignmentDecisionMaker.setTraining(isTraining);
	}

	public BoardCell getPosition() {
		return position;
	}

	public void setPosition(BoardCell position) {
		this.position = position;
	}

	public ForceEnum getForceType() {
		return forceType;
	}

	public void setForceType(ForceEnum forceType) {
		this.forceType = forceType;
	}

	public int getWeaponCount() {
		return weaponCount;
	}

	public void setWeaponCount(int weaponCount) {
		this.weaponCount = weaponCount;
	}

	public BufferedImage getImage() {
		return image;
	}

	public void setImage(BufferedImage image) {
		this.image = image;
	}

	public void shoot(int targetID) {

		if (weaponCount > 0) {
			List<ThreatStruct> threatList = Utilities.filterThreats(instance.getThreatList());
			for (ThreatStruct threat : threatList) {
				if (threat.getThreatID() == targetID) {
					threat.setDestroyed(true);
					weaponCount--;
					
				}
			}
		}
	}
	
	public Map<Integer,ThreatClassificationEnum> estimateClassification(){
		Map<Integer,ThreatClassificationEnum> classificationMap = new HashMap<Integer, ThreatClassificationEnum>();
		for(ThreatStruct threat : Utilities.filterThreats(instance.getThreatList())){
			ThreatClassificationEnum classification = threatEvaluationNeuralNetwork.getClassificationForThreat(threat);
			classificationMap.put(threat.getThreatID(), classification);
		}
		return classificationMap;
	}
	
	public int getWeaponAssignedThreatID(Map<Integer, ThreatEvaluationResultStruct> state){
		
		return weaponAssignmentDecisionMaker.getWeaponAssignedThreatID(state);
	}
	
	public void setWeaponAssignedThreatIDForOtherAgent(Map<Integer, ThreatEvaluationResultStruct> state, int threatID){
		weaponAssignmentDecisionMaker.setWeaponAssignedThreatIDForOtherAgent(state, threatID);
	}

	public int getAgentID() {
		return agentID;
	}

	public boolean isDestroyed() {
		return destroyed;
	}

	public void setDestroyed(boolean isDestroyed) {
		
		destroyed = isDestroyed;
	}

	public void save(ScenarioFinalStateEnum finalState){
		threatEvaluationNeuralNetwork.saveNetworkWeights();
		weaponAssignmentDecisionMaker.scenarioFinished(finalState);
	}
}
